/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.giftmemo.ui.view;

import com.alee.extended.date.WebDateField;
import com.alee.laf.combobox.WebComboBox;
import com.alee.laf.label.WebLabel;
import com.alee.laf.text.WebTextField;
import com.giftmemo.objects.Gender;
import com.giftmemo.objects.Person;
import com.giftmemo.objects.TypeOfRelationship;
import com.giftmemo.objects.impl.PersonImpl;
import com.giftmemo.ui.utils.ButtonsPanel;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

/**
 *
 * @author Egor
 */
public class PersonInfoPanel extends JPanel {

    private WebTextField personNameTextField;
    private WebComboBox typeOfRelationshipComboBox;
    private WebDateField dateOfBirthdayDateField;
    private WebComboBox genderComboBox;
    private WebLabel nameLabel;
    private WebLabel dateOfBirthdayLabel;
    private WebLabel genderLabel;
    private WebLabel typeOfRelationshipLabel;
    private Person person;
    private ButtonsPanel buttonsPanel;
    
    public PersonInfoPanel() {
        personNameTextField = new WebTextField();
        typeOfRelationshipComboBox = new WebComboBox();
        dateOfBirthdayDateField = new WebDateField();
        genderComboBox = new WebComboBox();

        nameLabel = new WebLabel();
        dateOfBirthdayLabel = new WebLabel();
        genderLabel = new WebLabel();
        typeOfRelationshipLabel = new WebLabel();
        person = new PersonImpl();
        buttonsPanel = new ButtonsPanel(ButtonsPanel.ButtonsPanelState.OK_BUTTON, ButtonsPanel.ButtonsPanelState.CANCEL_BUTTON);
        init();
    }

    private void init() {
        nameLabel.setText("Name: ");
        dateOfBirthdayLabel.setText("Birthday Date: ");
        genderLabel.setText("Gender: ");
        typeOfRelationshipLabel.setText("Relationship type: ");

        fillComboboxes();

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        Box nameBox = Box.createHorizontalBox();
        nameBox.add(nameLabel);
        nameBox.add(Box.createHorizontalStrut(5));
        nameBox.add(personNameTextField);
        add(nameBox);

        Box dbAndGenderBox = Box.createHorizontalBox();
        dbAndGenderBox.add(dateOfBirthdayLabel);
        dbAndGenderBox.add(Box.createHorizontalStrut(5));
        dbAndGenderBox.add(dateOfBirthdayDateField);
        dbAndGenderBox.add(Box.createHorizontalStrut(15));
        dbAndGenderBox.add(genderLabel);
        dbAndGenderBox.add(Box.createHorizontalStrut(5));
        dbAndGenderBox.add(genderComboBox);

        add(dbAndGenderBox);

        Box typeOfRelationshipBox = Box.createHorizontalBox();
        typeOfRelationshipBox.add(typeOfRelationshipLabel);
        typeOfRelationshipBox.add(Box.createHorizontalStrut(5));
        typeOfRelationshipBox.add(typeOfRelationshipComboBox);
        add(typeOfRelationshipBox);

        add(buttonsPanel);
    }

    
    private void fillComboboxes() {
        Gender[] genderValues = Gender.values();
        for (int i = 0; i < genderValues.length; i++) {
            genderComboBox.addItem(genderValues[i]);
        }

        TypeOfRelationship[] types = TypeOfRelationship.values();
        for (int i = 0; i < types.length; i++) {
            typeOfRelationshipComboBox.addItem(types[i]);
        }
    }

    private void fillInfo(Person person) {
        personNameTextField.setText(person.getPersonName());
        dateOfBirthdayDateField.setDate(person.getPersonBirthdayDate());
        typeOfRelationshipComboBox.setSelectedItem(person.getTypeOfRelationship());
        genderComboBox.setSelectedItem(person.getPersonGender());

    }

    public void setPerson(Person person) {
        this.person = person;
        fillInfo(this.person);
    }

    public Person getPerson() {
        person.setPersonName(personNameTextField.getText());
        person.setPersonBirthdayDate(dateOfBirthdayDateField.getDate());
        person.setPersonGender(Gender.FAMALE);
        person.setTypeOfrelationship(TypeOfRelationship.FRIEND);
        return person;
    }
    
    
    public void setAddGiftAction(Action action){
        buttonsPanel.setOkButtonAction(action);
    }
    
    public void setCancelAction(Action action){
        buttonsPanel.setCancelButtonAction(action);
    }
}
